package com.labManage.controller;

import com.labManage.dao.GoodsDao;
import com.labManage.entity.Goods;
import com.labManage.entity.User;
import com.labManage.service.GoodService;
import com.labManage.vo.GoodsVo;
import com.labManage.vo.ResultVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodService goodService;

    @Autowired
    GoodsDao goodsDao;
    @RequestMapping("/getAllGoodsByKindsId")
    @ResponseBody
    public ResultVo getAllGoodsByKindsId(String kindsId){
        if(null!=kindsId){
            List<Goods> goods = goodService.queryAllGoodsByKindsId(kindsId);
            if(null!=goods){
                return ResultVo.build(goods.size(),0,"",goods);
            }else{
                return ResultVo.build(200,"");
            }
        }else {
            return ResultVo.build(200,"没有该种类");
        }
    }

    @RequestMapping("/getAllGoods")
    @ResponseBody
    public ResultVo getAllGoods(Integer page,Integer limit,GoodsVo condition,String kindsIds){
        List<Long> kindsIdsList = null;
        if(kindsIds != null && !kindsIds.trim().equals("")){
            String[] array = kindsIds.split(",");
            kindsIdsList = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                kindsIdsList.add(Long.parseLong(array[i]));
            }
        }
        List<GoodsVo> goodsVoList = goodService.queryAllGoods(page,limit,condition,kindsIdsList);
        if(null!=goodsVoList){
            return ResultVo.build((int) goodService.getGoodsNum(condition,kindsIdsList),0,"",goodsVoList);
        }else {
            return ResultVo.build(201,null);
        }
    }

    @RequestMapping("deleteGoodsInfo")
    @ResponseBody
    @RequiresPermissions("goods:delete")
    public ResultVo deleteGoodsInfo(Goods goods){
        if(goods!=null){
            int result = goodService.deleteGoods(goods.getId());
            if(1==result){
                return ResultVo.build(0,"删除成功");
            }else if(2==result) {
                return ResultVo.build(201,"物品正在借阅中，不能删除");
            }else if(0==result) {
                return ResultVo.build(500,"服务器异常");
            }else {
                return ResultVo.build(500,"服务器异常");
            }
        }else{
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/getKindsNameByGoodsId")
    @ResponseBody
    public ResultVo getKindsNameByGoodsId(Long goodsId){
        String kindsName = goodsDao.selectKindsNameByGoodsId(goodsId);
        if(null!=kindsName){
            return ResultVo.build(0,"",kindsName);
        }else {
            return ResultVo.build(201,"",null);
        }
    }

    @RequestMapping("/getGoodsNum")
    @ResponseBody
    public ResultVo getGoodsNum(){
        long goodsNum = goodService.getGoodsNum(new GoodsVo(),null);
        return ResultVo.build(0,"",goodsNum);
    }

    @RequestMapping("/getStoreGoodsNum")
    @ResponseBody
    public ResultVo getStoreGoodsNum(){
        long goodsStoreNum = goodService.getStoreGoodsNum();
        return ResultVo.build(0,"",goodsStoreNum);
    }

    @RequestMapping("/commitGoodsModifyInfo")
    @ResponseBody
    @RequiresPermissions("goods:modify")
    public ResultVo commitGoodsModifyInfo(Goods goods){
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User)subject.getPrincipal();
        goods.setModifyTime(new Date());
        goods.setModifyPerson(loginUser.getUsername());
        Goods selectGoods = goodsDao.selectGoodsByGoodsNo(goods.getGoodsNo());
        if(selectGoods!=null){
            if(selectGoods.getId() == goods.getId()){
                int result = goodsDao.updateGoods(goods);
                if(0<result){
                    return ResultVo.build(0,"修改成功");
                }else{
                    return ResultVo.build(201,"修改失败");
                }
            }else{
                return ResultVo.build(201,"物品编号已存在");
            }
        }else{
            int result = goodsDao.updateGoods(goods);
            if(0<result){
                return ResultVo.build(0,"修改成功");
            }else{
                return ResultVo.build(201,"修改失败");
            }
        }
    }

    @RequestMapping("/goodsHandleState")
    @ResponseBody
    @RequiresPermissions("goods:borrow")
    public ResultVo goodsHandleState(Long goodsId,Long selectBorrowNum){
         if(null!=goodsId){
             int result = goodService.goodsHandleState(goodsId,selectBorrowNum);
             if(0==result){
                 return ResultVo.build(0,"操作成功");
             }else if(1==result) {
                 return ResultVo.build(201,"操作失败");
             }
             else if(2==result) {
                 return ResultVo.build(201,"非法访问");
             }
             else if(3==result) {
                 return ResultVo.build(500,"服务器异常");
             }else if(4==result){
                 return ResultVo.build(201,"库存不足");
             }else{
                 return ResultVo.build(500,"服务器异常");
             }
         }else {
             return ResultVo.build(500,"服务器异常");
         }
    }

    @RequestMapping("/addGoodsInfo")
    @ResponseBody
    @RequiresPermissions("goods:add")
    public ResultVo addGoodsInfo(Goods goods){
        if(null!=goods){
            if(""==goods.getGoodsName()){
                return ResultVo.build(201,"物品名字不能为空");
            }
            if(null==goods.getGoodsNum()){
                return ResultVo.build(201,"物品数量不能为空");
            }
            if(""==goods.getGoodsNo()){
                return ResultVo.build(201,"物品编号不能为空");
            }
            if(""==goods.getGoodsPosition()){
                return ResultVo.build(201,"物品位置不能为空");
            }
            int result = goodService.addGoodsInfo(goods);
            if(result==1){
                return ResultVo.build(0,"添加成功");
            }else if(result==2) {
                return ResultVo.build(201,"该物品编号已存在");
            }else if(result==3) {
                return ResultVo.build(201,"非耗材类物品数量必须为1");
            }else if(result==4) {
                return ResultVo.build(201,"请输入正确的耗材类物品数量");
            }else{
                return ResultVo.build(500,"服务器异常");
            }
        }else {
            return ResultVo.build(500,"服务器异常");
        }
    }
}
